<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Navs;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class NavsController extends Controller
{
    /**
     * Display a listing of the resource.
     * | GET|HEAD| Admin/navs | Admin.article.index
     * @return \Illuminate\Http\Response
     *  显示导航类表
     */
    public function index()
    {
        $data = Navs::orderBy('navs_order','asc')->paginate(10);
        return view('Admin.navs.index',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     * | GET|HEAD| Admin/article/create | Admin.article.create
     * @return \Illuminate\Http\Response
     * 显示文章添加页面
     */
    public function create()
    {
        return view('Admin.navs.add');
    }

    /**
     * Store a newly created resource in storage.
     * | POST| Admin/articleAdmin/article| Admin.article.store|
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     * 执行文章添加
     */
    public function store(Request $request)
    {
        $input = Input::except('_token');
        $rules = [
            'navs_name'=>'required',
            'navs_url'=>'required',
        ];
        $message = [
            'navs_name.required'=>'导航名称不能为空',
            'navs_url.required'=>'导航地址不能为空',
        ];
        $validator = Validator::make($input,$rules,$message);
        if($validator -> passes())
        {
            $res = Navs::create($input);
            if ($res) {
                return redirect('Admin/navs')->with('info', '导航添加成功');
            } else {
                return back()->with('info', '数据添加失败，请稍后再试');
            }
        }else
        {
            return back()->withErrors($validator);
        }
    }

    /**
     * Show the form for editing the specified resource.
     * | GET|HEAD| Admin/article/{article}/edit| Admin.article.edit
     * @param  int  $id
     * @return \Illuminate\Http\Response
     * 显示文章修改页面
     */
    public function edit($id)
    {
        $data = Navs::find($id);
        return view('Admin.navs.edit',compact('data'));
    }

    /**
     * Update the specified resource in storage.
     * | PUT|PATCH | Admin/article/{article} | Admin.article.update
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     * 执行文章更新
     */
    public function update(Request $request, $id)
    {
        $input = Input::except('_token','_method');
        $res = Navs::where('navs_id',$id)->update($input);
        if($res)
        {
            return redirect('Admin/navs')->with('info','导航更新成功');
        }else
        {
            return back()->with('info','导航更新失败，请稍候再试');
        }
    }

    /**
     * Remove the specified resource from storage.
     * | DELETE | Admin/article/{article} | Admin.article.destroy
     * @param  int  $id
     * @return \Illuminate\Http\Response
     * 执行文章删除
     */
    public function destroy($id)
    {
        $res = Navs::where('navs_id',$id)->delete();
        if($res)
        {
            $data = [
                'status'=>0,
                'msg'=>'分类删除成功',
            ];
        }else
        {
            $data = [
                'status'=>1,
                'msg'=>'分类删除失败，请稍候再试',
            ];
        }
        return $data;
    }

    public function changeorder()
    {
        $input = Input::all();
        $navs = Navs::find($input['navs_id']);
        $navs -> navs_order = $input['navs_order'];
        $res = $navs->update();
        if($res)
        {
            $date = [
                'status' => 0,
                'msg' => '自定义导航导航排序更新成功',
            ];
        }else
        {
            $date = [
                'status' => 1,
                'msg' => '导航更新失败，请稍候再试',
            ];
        }
        return $date;
    }
}
